package JAVA300.第十章IO;
/**
 * 将文件拷贝封装成方法--引入try-with-resource
 * 自动释放资源
 */

import java.io.*;

public class IO_Study05_method2 {

    public static void main(String[] args) {
        CopyFile("D:\\Java300_Exp\\src\\JAVA300\\第十章IO\\IO_Study05.java","copyJAVA.txt");

    }

    public static void CopyFile(String srcPath,String destPath){
        File src = new File(srcPath);
        File dest = new File(destPath);

        //将流的声明放在里面
        try(InputStream is = new FileInputStream(src);
            OutputStream os = new FileOutputStream(dest);) {

            int len;
            byte[] flush = new byte[1024];
            while ((len = is.read(flush)) != -1){
                os.write(flush,0,len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
